import java.util.ArrayList;
import java.util.List;

//leetcode 257 二叉树的所有路径
public class Test4 {
    List<String> list;
    public List<String> binaryTreePaths(TreeNode root) {
        list = new ArrayList<>();
        dfs(root, new StringBuilder());
        return list;
    }

    private void dfs(TreeNode root, StringBuilder s) {
        if (root == null) return;
        StringBuilder sb = new StringBuilder(s);
        sb.append(root.val);
        if (root.left == null && root.right == null) {
            list.add(sb.toString());
            return;
        }
        sb.append("->");
        dfs(root.left, sb);
        dfs(root.right, sb);
    }
}
